package com.competition.reception.controller;


import com.competition.commonutils.JwtUtils;
import com.competition.commonutils.R;
import com.competition.reception.entity.Module;
import com.competition.reception.entity.vo.ModuleQueryVo;
import com.competition.reception.service.ModuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 投稿信息表 前端控制器
 * </p>
 * @author testjava
 * @since 2021-04-03
 */
@RestController
@RequestMapping("/reception/module")
@CrossOrigin
@Api(description = "后台模块相关的接口")
public class ModuleController {

    @Resource
    private ModuleService moduleService;

    @ApiOperation("根据稿子id查询对应稿子")
    @GetMapping("selectModuleById/{id}")
    public R selectModuleById(@PathVariable("id") String id){
        Module module = moduleService.getById(id);
        return R.ok().data("module",module);
    }

    @ApiOperation("根据模块id删除对应模块")
    @DeleteMapping("deleteModuleById/{id}")
    public R deleteModuleById(@PathVariable("id") String id){
        moduleService.removeById(id);
        return R.ok();
    }

    @ApiOperation("后台管理投稿")
    @PostMapping("addModule")
    public R addModule(@RequestBody Module module, HttpServletRequest request){
        System.out.println(module);
        String manageId = JwtUtils.getMemberIdByJwtToken(request);
        module.setUserId(manageId);
        String id=getRandom620(20);
        module.setId(id);
        boolean save = moduleService.save(module);
        return save==true?R.ok().data("id",id): R.error();
    }
    @ApiOperation("修改稿子")
    @PostMapping("updateModule")
    public R updateModule(@RequestBody Module module){
        boolean update = moduleService.update(module, null);
        return update==true?R.ok(): R.error();
    }

    /**
     *
     * @param id
     * @param isCheck 通过(0) 未通过(1)
     * @return
     */
    @ApiOperation("设置审核")
    @PostMapping("statusManage/{id}/{status}")
    public R statusManage(@PathVariable("id")String id,@PathVariable("status") Integer status){
        moduleService.statusManage(id,status);
        return R.ok();
    }
    @ApiOperation("//高级查询模块\n" +
            "    /**\n" +
            "     *  id 根据稿子id\n" +
            "     * title 根据标题\n" +
            "     * username 根据用户名\n" +
            "     * startTime-endTime  根据投稿时间\n" +
            "     */")
    @PostMapping("selectByCondition")
    public R selectByCondition(@RequestBody ModuleQueryVo moduleQueryVo){
        Map<String, Object> resultMap = moduleService.selectByConditions(moduleQueryVo);
        return R.ok().data("resultMap",resultMap);
    }

    //生成随机数
    public static String getRandom620(Integer length) {
        String result = "";
        Random rand = new Random();
        int n = 20;
        if (null != length && length > 0) {
            n = length;
        }
        int randInt = 0;
        for (int i = 0; i < n; i++) {
            randInt = rand.nextInt(10);

            result += randInt;
        }
        return result;
    }

}

